# coding=utf-8

from scene import Scene
import pygame
import pygame_gui

import group1
import group2
import group3

WIN_WIDTH = 1600
WIN_HEIGHT = 800

LABEL_HEIGHT = WIN_HEIGHT // 7
ANSWER_HEIGHT = LABEL_HEIGHT
QUESTION_HEIGHT = 2 * LABEL_HEIGHT

#ques_lib = group1.gen_ques(50, swap=True)
#ques_lib = group2.gen_ques(50, category=20)
ques_lib = group3.gen_ques(50, swap=True)

class SceneTestResult(Scene):

    def __init__(self):
        super().__init__()
        self.ques_idx = 0
        self.ques_new = True
        self.answ_idx = -1
        self.answ_sel_idx = -1

        self.ques_count = 0
        self.correct_count = 0
        self.wrong_count = 0

        # 创建 GUI 管理器
        self.manager = pygame_gui.UIManager((WIN_WIDTH, WIN_HEIGHT), 'theme.json')

        self.label_result = pygame_gui.elements.UILabel(
            relative_rect=pygame.Rect((0, 100), (WIN_WIDTH, LABEL_HEIGHT)),
            text="第 1 题",
            manager=self.manager,
            object_id=pygame_gui.core.ObjectID(object_id='#label_no')
        )

        self.label_comment = pygame_gui.elements.UILabel(
            relative_rect=pygame.Rect((0, 0), (WIN_WIDTH, LABEL_HEIGHT)),
            text="正确数：0",
            manager=self.manager,
            object_id=pygame_gui.core.ObjectID(object_id='#label_correct'),
             anchors = {"bottom": "top",
                        # "right": "left",
                        "top_target": self.label_result}
        )

        self.button_again = pygame_gui.elements.UIButton(
            relative_rect=pygame.Rect((0, 100), (WIN_WIDTH//2, LABEL_HEIGHT)),
            text="再做一次",
            manager=self.manager,
            object_id=pygame_gui.core.ObjectID(object_id='#button_again'),
             anchors = {"bottom": "top",
                        "centerx": "centerx",
                        # "right": "left",
                        "top_target": self.label_comment}
        )

        # self.label_wrong = pygame_gui.elements.UILabel(
        #     relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
        #     text="错误数：0",
            #manager=self.manager,
            #object_id=pygame_gui.core.ObjectID(object_id='#label_wrong'),
            # anchors = {"top": "top",
            #            "right": "left",
            #            "left_target": self.label_correct}
        #)

        #self.label_time = pygame_gui.elements.UILabel(
            #relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
            #text="时间：00:00",
            #manager=self.manager,
            #object_id=pygame_gui.core.ObjectID(object_id='#label_time'),
            # anchors = {"top": "top",
            #            "right": "left",
            #            "left_target": self.label_wrong}
        #)

        #self.textbox_question = pygame_gui.elements.UITextBox(
            #relative_rect=pygame.Rect((0, LABEL_HEIGHT), (WIN_WIDTH, QUESTION_HEIGHT)),
            #html_text="时间：00:00",
            #manager=self.manager,
            #object_id=pygame_gui.core.ObjectID(object_id='#text_box_question'),
            ##  anchors = {"top": "top",
            ##             "right": "left",
            ##             "left_target": label_wrong}
        #)

        #self.button_answers = []
        #for i in range(4):
            #button_answer = pygame_gui.elements.UIButton(
            #    relative_rect=pygame.Rect((0, i*ANSWER_HEIGHT + QUESTION_HEIGHT + LABEL_HEIGHT), (WIN_WIDTH, ANSWER_HEIGHT)),
            #    text="",
            #    manager=self.manager,
            #    object_id=pygame_gui.core.ObjectID(class_id='@button_answer_left' if i % 2 == 0 else '@button_answer_right')
            #                                       #object_id='#hello_button')#,
            #    # object_id=pygame_gui.core.ObjectID(object_id='#label_time'),
            #    #  anchors = {"top": "top",
            #    #             "right": "left",
            #    #             "left_target": label_wrong}
            #)
            #self.button_answers.append(button_answer)

    def activate(self, params = None):
        # self.ques_lib = ques_lib
        print("params = ", params)
        # if params:
        assert params
        self.activate_params = params
        self.label_result.set_text(f"一共 {params['total_count']} 题，做对 {params['correct_count']} 题，做错 {params['wrong_count']} 题。")
# 100分: 完美无缺！你的努力和才智令人钦佩！
# 90分以上: 非常出色！继续保持，你的表现非常优秀！
# 70分以上: 做得不错！还有进步的空间，加油！
# 70分以下: 不要气馁，下次会更好！继续努力，你一定能取得更好的成绩！
        if params['wrong_count'] == 0:
            self.label_comment.set_text("完美无缺！你的努力和才智令人钦佩！")
        else:
            score = 100 * (params['total_count'] - params['wrong_count']) / params['total_count']
            print("score = ", score)
            if score >= 90:
                self.label_comment.set_text("非常出色！继续保持，你的表现非常优秀！")
            elif score >= 70:
                self.label_comment.set_text("做得不错！还有进步的空间，加油！")
            else:
                self.label_comment.set_text("不要气馁，下次会更好！继续努力，你一定能取得更好的成绩！")


    def select_correct(self):
        # self.correct_count += 1
        # self.label_correct.set_text("正确数：%d" % self.correct_count)
        print("correct!")


    def select_wrong(self):
        # self.wrong_count += 1
        # self.label_wrong.set_text("错误数：%d" % self.wrong_count)
        print("wrong!")


    def process_events(self, events):
        # if self.ques_new:
        #     print("ques_idx = %d, len(ques_lib) = %d" % (self.ques_idx, len(self.ques_lib)))
        #     ques = self.ques_lib[self.ques_idx]

        #     self.textbox_question.set_text(ques[0])
        #     for ans_idx in range(0, 4):
        #         self.button_answers[ans_idx].set_text(ques[ans_idx + 1])
        #         self.button_answers[ans_idx].enable()

        #     self.answ_idx = ques[5]
        #     self.ques_idx += 1

        #     self.label_no.set_text("第 %d 题" % self.ques_idx)

        #     self.ques_new = False

        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    print("A key pressed")
                if event.key == pygame.K_b:
                    print("B key pressed")

            elif event.type == pygame_gui.UI_BUTTON_PRESSED:
                if event.ui_element == self.button_again:
                    print('again!')
                    # params = {}
                    # params["test_
                    return self.switch("test", self.activate_params["test_params"])
                # answ_sel_idx = -1
                # for i, button_answer in enumerate(self.button_answers):
                #     if event.ui_element == button_answer:
                #         answ_sel_idx = i
                #         if answ_sel_idx == self.answ_idx:
                #             self.select_correct()
                #             self.ques_new = True
                #         else:
                #             button_answer.disable()
                #             self.select_wrong()
                #         break

            self.manager.process_events(event)


    def update(self, time_delta):
        self.manager.update(time_delta)


    def draw(self, screen):
        self.manager.draw_ui(screen)
